package cn.momoshenchi.product.service.impl;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.momoshenchi.common.utils.PageUtils;
import cn.momoshenchi.common.utils.Query;

import cn.momoshenchi.product.dao.SkuInfoDao;
import cn.momoshenchi.product.entity.SkuInfoEntity;
import cn.momoshenchi.product.service.SkuInfoService;


@Service("skuInfoService")
public class SkuInfoServiceImpl extends ServiceImpl<SkuInfoDao, SkuInfoEntity> implements SkuInfoService
{

    @Override
    public PageUtils queryPage(Map<String, Object> params)
    {
        IPage<SkuInfoEntity> page = this.page(
                new Query<SkuInfoEntity>().getPage(params),
                new QueryWrapper<SkuInfoEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPageByCondition(Map<String, Object> params)
    {
        QueryWrapper<SkuInfoEntity> wrapper = new QueryWrapper<>();
        String key = (String) params.get("key");
        if (!StringUtils.isBlank(key))
        {
            wrapper.and(w -> {
                w.eq("sku_id", key).or().like("sku_name", key);
            });

        }
        String brandId = (String) params.get("brandId");
        if (!StringUtils.isBlank(brandId) &&!"0".equalsIgnoreCase(brandId))
        {
            wrapper.eq("brand_id", brandId);
        }
        String categoryId = (String) params.get("categoryId");
        if (!StringUtils.isBlank(brandId) &&!"0".equalsIgnoreCase(categoryId))
        {
            wrapper.eq("category_id", categoryId);
        }
        String min = (String) params.get("min");
        String max = (String) params.get("max");

        if (!StringUtils.isBlank(min))
        {
            wrapper.ge("price", min);
        }
        if (!StringUtils.isBlank(max)&& !"0".equals(max))
        {
            wrapper.le("price", max);
        }
        IPage<SkuInfoEntity> page = this.page(
                new Query<SkuInfoEntity>().getPage(params), wrapper
        );

        return new PageUtils(page);


    }

    @Override
    public List<SkuInfoEntity> getBySpuId(Long spuId)
    {

        List<SkuInfoEntity> skuInfoEntities = list(new QueryWrapper<SkuInfoEntity>().eq("spu_id", spuId));
        return  skuInfoEntities;

    }

}